package cat.sun.dialogs;

import java.util.Random;

import cat.sun.R;
import cat.sun.activities.Game;
import cat.sun.data.MyInteger;
import cat.sun.stats.StatsValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class DialogPlay extends DialogFragment implements OnClickListener {
	public static DialogPlay newInstance() {
		DialogPlay dialog = new DialogPlay();
		return dialog;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_FRAME, 0);
		// setCancelable(false);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dialog_play, container, false);
		final Context context = getActivity();
		
		View item1 = view.findViewById(R.id.dialog_play_item1);
		TextView name1 = (TextView) item1.findViewById(R.id.dialog_play_level);
		name1.setText(context.getString(R.string.EasyLevel));
		TextView bombscount1 = (TextView) item1.findViewById(R.id.dialog_play_bombs);
		bombscount1.setText("10");
		TextView fieldcount1 = (TextView) item1.findViewById(R.id.dialog_play_size);
		fieldcount1.setText("9x9");
		name1.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf"));
		
		View item2 = view.findViewById(R.id.dialog_play_item2);
		TextView name2 = (TextView) item2.findViewById(R.id.dialog_play_level);
		name2.setText(context.getString(R.string.MediumLevel));
		TextView bombscount2 = (TextView) item2.findViewById(R.id.dialog_play_bombs);
		bombscount2.setText("40");
		TextView fieldcount2 = (TextView) item2.findViewById(R.id.dialog_play_size);
		fieldcount2.setText("16x16");
		name2.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf"));
		
		View item3 = view.findViewById(R.id.dialog_play_item3);
		TextView name3 = (TextView) item3.findViewById(R.id.dialog_play_level);
		name3.setText(context.getString(R.string.InsainLevel));
		TextView bombscount3 = (TextView) item3.findViewById(R.id.dialog_play_bombs);
		bombscount3.setText("99");
		TextView fieldcount3 = (TextView) item3.findViewById(R.id.dialog_play_size);
		fieldcount3.setText("16x30");
		name3.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf"));
		
		View item4 = view.findViewById(R.id.dialog_play_item4);
		TextView name4 = (TextView) item4.findViewById(R.id.dialog_play_level);
		name4.setText(context.getString(R.string.RandomLevel));
		TextView bombscount4 = (TextView) item4.findViewById(R.id.dialog_play_bombs);
		bombscount4.setText("??");
		TextView fieldcount4 = (TextView) item4.findViewById(R.id.dialog_play_size);
		fieldcount4.setText("??x??");
		name4.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf"));		

		TextView title = (TextView) view.findViewById(R.id.dialog_play_title);
		title.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf"));

		final ImageView close = (ImageView) view.findViewById(R.id.dialog_play_close);

		close.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				dismiss();
			}
		});		
		
		item1.setOnClickListener(this);
		item2.setOnClickListener(this);
		item3.setOnClickListener(this);
		item4.setOnClickListener(this);
		
		
		TextView customTitle = (TextView) view.findViewById(R.id.dialog_play_level_custom);	
		customTitle.setText(getResources().getString(R.string.CustomLevel));
		customTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
				
		final SeekBar width = (SeekBar) view.findViewById(R.id.dialog_play_width);
		final SeekBar height = (SeekBar) view.findViewById(R.id.dialog_play_height);
		final SeekBar bombs = (SeekBar) view.findViewById(R.id.dialog_play_bombs_count);
		final TextView tSize = (TextView) view.findViewById(R.id.dialog_play_size_custom);				
		final TextView tBombs = (TextView) view.findViewById(R.id.dialog_play_bombs_custom);		
		final Button play = (Button) view.findViewById(R.id.dialog_play_play_custom);
		
		final MyInteger currWidth = new MyInteger(1);
		final MyInteger currHeight = new MyInteger(1);
		final MyInteger currBombs = new MyInteger(0);
		
		tSize.setText(String.valueOf(currWidth.getValue())+"x"+String.valueOf(currHeight.getValue()));
		tSize.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		tBombs.setText(currBombs.getStringValue());
		tBombs.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		width.setProgress(currWidth.getValue()-1);
		height.setProgress(currHeight.getValue()-1);
		bombs.setProgress(currBombs.getValue());
		bombs.setMax(currHeight.getValue()*currWidth.getValue() - 1);
		
		play.setOnClickListener(new OnClickListener() 
		{					
			@Override
			public void onClick(View v) 
			{
				dismiss();
				Intent intent = new Intent(context,Game.class);
				intent.putExtra("width", width.getProgress()+1);
				intent.putExtra("height", height.getProgress()+1);
				intent.putExtra("bombs", bombs.getProgress());
				intent.putExtra("Table", StatsValues.CUSTOM_KEY);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);						
				context.startActivity(intent);
				
			}
		});
		play.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		tBombs.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		tSize.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));		
		
		width.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
		{	
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
			{			
				currWidth.setValue(progress+1) ;
				tSize.setText(currWidth.getStringValue()+"x"+currHeight.getStringValue());
				bombs.setMax((currWidth.getValue())*(currHeight.getValue())-1);
			}
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar)
			{		
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar)
			{
			}				
	
		});
		height.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
		{	
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
			{			
				currHeight.setValue(progress+1) ;
				tSize.setText(currWidth.getStringValue()+"x"+currHeight.getStringValue());
				bombs.setMax((currWidth.getValue())*(currHeight.getValue())-1);
			}
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar)
			{		
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar)
			{
			}					
	
		});			
		bombs.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
		{	
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
			{
				tBombs.setText(String.valueOf(progress));	
			}
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar)
			{
				currBombs.setValue(bombs.getProgress());
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar){}				
	
		});	
		
		return view;
	}
	

	@Override
	public void onClick(View v) {
		dismiss();
		Intent intent = new Intent(getActivity(), Game.class);
		switch (v.getId()) {
		case R.id.dialog_play_item1: {
			intent.putExtra("width", 9);
			intent.putExtra("height", 9);
			intent.putExtra("bombs", 10);
			intent.putExtra("Table", StatsValues.EASY_KEY);
			break;
		}
		case R.id.dialog_play_item2: {
			intent.putExtra("width", 16);
			intent.putExtra("height", 16);
			intent.putExtra("bombs", 40);
			intent.putExtra("Table", StatsValues.MEDIUM_KEY);
			break;
		}
		case R.id.dialog_play_item3: {
			intent.putExtra("width", 16);
			intent.putExtra("height", 30);
			intent.putExtra("bombs", 99);
			intent.putExtra("Table", StatsValues.INSANE_KEY);
			break;
		}
		case R.id.dialog_play_item4: 
		{
			Random r = new Random();
			int w = r.nextInt(30) + 1;
			int h = r.nextInt(30) + 1;
			int b = r.nextInt(h * w);
			intent.putExtra("width", w);
			intent.putExtra("height", h);
			intent.putExtra("bombs", b);
			intent.putExtra("Table", StatsValues.RANDOM_KEY);
			break;
		}		
		}
		startActivity(intent);
		
	}

}

